<template>
  <div v-if="readonly" class="itemNames">
    <el-tag v-for="item in chioses" :key="item.Value" effect="plain">{{ item.Text }}</el-tag>
  </div>
  <el-checkbox-group v-else-if="mode == 'button' && multiple == true && curVal != null" v-model="curVal" :disabled="disabled" :style="styleSet" @change="chooseChange">
    <el-checkbox-button v-for="item in items" :key="item.Value" :label="item.Value">{{ item.Text }}</el-checkbox-button>
  </el-checkbox-group>
  <el-select
    v-else-if="mode == 'select'"
    v-model="curVal"
    :disabled="disabled"
    :placeholder="place"
    :style="styleSet"
    :clearable="clearable"
    :class="className"
    :multiple="multiple"
    @change="chooseChange"
  >
    <el-option
      v-for="item in items"
      :key="item.Value"
      :label="item.Text"
      :value="item.Value"
    />
  </el-select>
  <el-radio-group
    v-else-if="mode == 'radio' && multiple == false"
    v-model="curVal"
    :disabled="disabled"
    @input="chooseChange"
  >
    <el-radio v-for="item in items" :key="item.Value" :label="item.Value">{{
      item.Text
    }}</el-radio>
  </el-radio-group>
  <el-radio-group
    v-else-if="mode == 'button' && multiple == false"
    v-model="curVal"
    :disabled="disabled"
    @input="chooseChange"
  >
    <el-radio-button
      v-for="item in items"
      :key="item.Value"
      :label="item.Value"
    >{{ item.Text }}</el-radio-button>
  </el-radio-group>
</template>

<script>
import { GetItems } from '@/api/base/enumDic'
export default {
  props: {
    dicKey: {
      type: String,
      default: null
    },
    clearable: {
      type: Boolean,
      default: true
    },
    readonly: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    className: {
      type: String,
      default: 'el-input'
    },
    value: {
      default: null
    },
    placeholder: {
      type: String,
      default: '选择字典'
    },
    filters: {
      type: Array,
      default: null
    },
    limit: {
      type: Array,
      default: null
    },
    filterFunc: {
      type: Function,
      default: null
    },
    multiple: {
      type: Boolean,
      default: false
    },
    styleSet: {
      type: Object,
      default: null
    },
    mode: {
      type: String,
      default: 'select'
    },
    sysHead: {
      type: String,
      default: 'hr'
    },
    enumItems: {
      type: Array,
      default: null
    }
  },
  data() {
    return {
      place: null,
      items: [],
      source: null,
      chioses: [],
      curVal: null
    }
  },
  watch: {
    dicKey: {
      handler(val) {
        if (val) {
          this.loadDict()
        }
      },
      immediate: true
    },
    filters: {
      handler(val) {
        this.resetFilter()
      },
      immediate: false
    },
    limit: {
      handler(val) {
        this.resetFilter()
      },
      immediate: false
    },
    value: {
      handler(val) {
        if (this.multiple) {
          this.curVal = Array.isArray(val) ? val : []
        } else {
          this.curVal = val
        }
        this.loadNames()
      },
      immediate: false
    },
    placeholder: {
      handler(val) {
        this.place = val
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    resetFilter() {
      if (this.source == null) {
        return
      }
      if (this.filterFunc) {
        this.items = this.filterFunc(this.source)
      } else if (this.filters) {
        this.items = this.source.filter((c) => !this.filters.includes(c.Value))
      } else if (this.limit) {
        this.items = this.source.filter((c) => this.limit.includes(c.Value))
      } else {
        this.items = this.source
      }
    },
    async loadDict() {
      const res = await GetItems(this.dicKey, this.sysHead)
      this.source = res
      this.resetFilter()
      if (this.multiple) {
        this.curVal = Array.isArray(this.value) ? this.value : []
      } else {
        this.curVal = this.value
      }
      this.$emit('update:enumItems', res)
      this.loadNames()
    },
    loadNames() {
      if (this.value == null || this.readonly === false) {
        this.chioses = []
        return
      } else if (this.multiple && this.value.length > 0) {
        this.chioses = this.items.filter(c => this.value.includes(c.Value))
      } else if (this.multiple === false) {
        this.chioses = this.items.filter(c => c.Value === this.value)
      } else {
        this.chioses = []
      }
    },
    chooseChange(value) {
      let item = null
      if (value != null) {
        if (this.multiple) {
          item = this.items.filter((c) => value.includes(c.Value))
        } else {
          item = this.items.find((c) => c.Value === value)
        }
      }
      this.$emit('input', value)
      this.$emit('change', value, item)
    }
  }
}
</script>

<style scoped>
.itemNames {
  display: inline-block;
  line-height: 20px;
}
.itemNames .el-tag {
  margin-right: 5px;
}
</style>
